#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include "Hw3Types.h"
#include "Hw3TypesExtended.h"
#include "GlobalVariables.h"
#include "IspImageAnalysis.h"
#include "IspImageAnalysisExtended.h"

//declarations
BOOL FindFacesInRowSimple(TArea *Area, int LineIndex);
BOOL ConvertImageToGrayScaleSimple();

//functions

//this function prints the facebook to the screen
void PrintFacebook()
{
	FILE* FacebookFile = fopen("Facebook.txt", "w"); 
	FILE* NumOfFacesFile = fopen("output_debug.txt", "w");
	TFace *CurrentFace = FacebookHead;
	int NumOfFaces = 0;
	while (CurrentFace != NULL)
	{
		NumOfFaces++;
		fprintf(FacebookFile, "%d,%d\n", CurrentFace->Box.OriginY, CurrentFace->Box.OriginX);
		CurrentFace = CurrentFace->NextFace;
	}
	fprintf(NumOfFacesFile, "Number of faces: %d", NumOfFaces);
	fclose(FacebookFile);
	fclose(NumOfFacesFile);
}

//this function prints the grayscale values to a file
void printgrayimage()
{													
	int row=0;											
	int col=0;											
	FILE	*GrayImageFile = NULL;								 
	GrayImageFile =fopen("GrayImage.txt","w");						
				
	for(row=0;row<ImageReaderAndProperties.Height;row++)						
	{												
		for(col=0;col<ImageReaderAndProperties.Width;col++)
		{
fprintf(GrayImageFile," %.3u ",(unsigned char)Image[row][col]);
		}
		fprintf(GrayImageFile,"\n");
	}
	fclose(GrayImageFile);
}

// these are simplified functions from the program, to allow easier functionallity testing (without synchronization issues)
BOOL FindFacesInAreaSimple(TArea *Area)
{
	int LineIndex;
	
	#ifdef DEBUG_MODE
		printf("Started searching for faces in area with horizontal index: %d and vertical index: %d\n", Area->HorizontalIndex, Area->VerticalIndex);
	#endif

	for (LineIndex=Area->OriginY ; LineIndex < (Area->OriginY+Area->Height) ; LineIndex++)
			FindFacesInRowSimple(Area, LineIndex);
	return TRUE; // do we need to return a value here?
}

BOOL FindFacesInRowSimple(TArea *Area, int LineIndex)
{
	TBox *FoundFaceBox=NULL;
	BOOL FoundFace=FALSE;
	int BoxLeft; 
	int BoxTop = LineIndex;

	#ifdef DEBUG_MODE
		printf("Started searching faces in line %d of area with horizInd %d and vertInd %d)\n", LineIndex, Area->HorizontalIndex, Area->VerticalIndex);
	#endif

	for (BoxLeft = Area->OriginX ; BoxLeft < Area->OriginX+Area->Width ; BoxLeft++) //SEE THAT THIS LOOKS IN LAST PIXEL OF THE AREA AS WELL
	{
		#ifdef DEBUG_MODE
			printf("Searching in line: %d, column: %d)\n", BoxTop, BoxLeft);
		#endif
		FoundFace = BoxContainsAFace(Image, BoxTop, BoxLeft, 
			FaceSize.Height, FaceSize.Width, 
			ImageReaderAndProperties.Height, 
			ImageReaderAndProperties.Width);
		if (FoundFace == TRUE)
		{
			#ifdef DEBUG_MODE
				printf("Found Face in location: (%d, %d)\n", BoxLeft, BoxTop);
			#endif
		}
	}	

	return TRUE;
}

BOOL ConvertImageToGrayScaleSimple()
{
	int RowIndex;
	Byte_t *RowBuffer = (Byte_t*)malloc(ImageReaderAndProperties.Pitch*sizeof(Byte_t));
	for(RowIndex = 0; RowIndex < ImageReaderAndProperties.Height ; RowIndex++)
	{
		ConvertRowToGrayScale(RowIndex, RowBuffer);
		#ifdef DEBUG_MODE
			if (RowIndex%100 == 0)
				printf("Converted row number %d to grayscale\n", RowIndex);
		#endif
	}
	free(RowBuffer);
	return TRUE;
}

// this function prints the areas division to the screen
void PrintAreasDivision(TArea *Areas, int NumOfAreasHorizontal, int NumOfAreasVertical)
{
		int i,j;
		printf("Pic height: %d ; Pic width: %d\nFace height: %d ; Face width: %d\n", ImageReaderAndProperties.Height, ImageReaderAndProperties.Width, FaceSize.Height, FaceSize.Width);
		printf("Number of horizontal areas: %d\nNumber of vertical areas: %d\n\n", NumOfAreasHorizontal, NumOfAreasVertical);
		for (i=0 ; i<NumOfAreasVertical ; i++)
		{
			for (j=0 ; j<NumOfAreasHorizontal ; j++)
			{
				printf("  %d:%d   ", Areas[NumOfAreasHorizontal*i+j].VerticalIndex, Areas[NumOfAreasHorizontal*i+j].HorizontalIndex);
			}
			printf("\n");
			for (j=0 ; j<NumOfAreasHorizontal ; j++)
			{
				printf(" (%d,%d)  ", Areas[NumOfAreasHorizontal*i+j].OriginX, Areas[NumOfAreasHorizontal*i+j].OriginY);
			}
			printf("\n");
			for (j=0 ; j<NumOfAreasHorizontal ; j++)
			{
				printf("   %dX%d  ", Areas[NumOfAreasHorizontal*i+j].Width, Areas[NumOfAreasHorizontal*i+j].Height);
			}
			printf("\n\n");
		}	
}


// this function prints a line of the image to the screen
void PrintLine(int LineNumber, IspImage Image)
{
	int j;
	for (j=0;j<ImageReaderAndProperties.Width;j++)
		printf("%d: %d ", j, Image[LineNumber][j]);
}
